var rowIndex = "%^%%%%";
var regid = 0;
var flag = true;
var page;
var _fEntryID;
var _fInterID;
var _fIcomEntryId;
var _fOperSN,_fSubcOutId;
/*
* @Description: 操作事件
* @param :
*/
window.operateEvents = {
		'click .details' : function(e, value, row, index) {//审核按钮点击事件
//			console.log(row)
			$(".clean_c").val("");
			_fEntryID = row.fEntryID;
			_fInterID = row.fInterID;
			_fIcomEntryId = row.fIcomEntryId;
			_fSubcOutId = row.fSubcOutId;
			_fOperSN = row.fOperSN;
			loadData(row,'#auditingForm');
			$("#auditingfngQty").val(parseFloat(row.fReceiveQty) - parseFloat(row.fPassQty));
			$("#auditingfDate").val(timeFormatter(row.fDate));
			$("#auditingfStockCheckQty").val($("#auditingfPassQty").val());
			$("#auditingfStockCheckQty").val("");
			$("#auditingngQty").val($("#auditingfngQty").val());
			$("#auditingngfAuxQtyScrap").val(row.fAuxQtyScrap);
			$("#auditingngfAuxQtyForItem").val(row.fAuxQtyForItem);
			$("#auditingngfOthQty").val(row.fOthQty);
			$("#auditingModal").modal('show');
		}
}
var resetColumn = [ 
	{
		field : 'fBillNo',
		align : 'left',
		valign: 'middle',
		sortable : true,
		title : '生产任务单',
		visible : true,
		must:true,
		width: '10%'
	}, {
		field : 'fSubcOutNo',
		align : 'left',
		valign: 'middle',
		sortable : true,
		title : '委外单号',
		visible : true,		
		width: '10%',
	}, {
		field : 'fSupplierName',
		align : 'left',
		valign: 'middle',
		sortable : true,
		title : '委外单位',
		visible : true,		
		width: '10%',
	},{
		field : 'fNumber',
		align : 'left',
		valign: 'middle',
		sortable : true,
		title : '产品代码',
		visible : true,
		width: '10%',
	},{
		field : 'fName',
		align : 'left',
		valign: 'middle',
		sortable : true,
		title : '产品名称',
		visible : true,
		width: '10%',
	},{
		field : 'fModel',
		align : 'left',
		valign: 'middle',
		sortable : true,
		title : '规格型号',
		visible : true,		
		width: '10%',
	}, {
		field : 'fOperSN',
		align : 'left',
		valign: 'middle',
		sortable : true,
		title : '工序号',
		visible : true,		
		width: '10%',
	}, {
		field : 'fOperName',
		align : 'left',
		valign: 'left',
//		sortable : true,
		title : '工序名称',
		visible : true,		
		width: '10%',
	}, {
		field : 'fBatchNo',
		align : 'left',
		valign: 'middle',
		title : '批号',
		formatter : isNull,
		visible : true,		
		width: '10%',
	}, {
		field : 'fReceiveQty',
		align : 'left',
		valign: 'middle',
		title : '接收数',
		formatter : isNull,
		visible : true,		
		width: '10%',
	},{
		field : 'fPassQty',
		align : 'left',
		valign: 'middle',
		sortable : true,
		title : '合格数',
		visible : true,
		width: '10%'
	},{
		field : 'NGQty',
		align : 'left',
		valign: 'middle',
		sortable : true,
		title : '不合格',
		visible : true,
		formatter: function(value, row, index) {
			return parseFloat(row.fReceiveQty) - parseFloat(row.fPassQty);
		},
		showSearch:false, // 搜索时是否显示
		width: '10%'
	}, {
		field : 'fWorkerName',
		align : 'left',
		valign: 'middle',
		sortable : true,
		title : '接收人',
		visible : true,		
		width: '10%'
	}, {
		field : 'fDate',
		align : 'left',
		valign: 'middle',
		sortable : true,
		title : '接收时间',
		visible : true,		
		width: '10%',
		formatter : dateFormatter
	}, {
		field : 'operate',
		align : 'left',
		valign: 'middle',
		title : '操作',
		formatter : isNull,
		visible : true,		
		width: '10%',
		must:true,
		formatter : function(value,row,index){
			return '<button type="button" class="btn btn-default details" style="font-size:12px;margin-right:5px;">审核</button>';
		},
		events : operateEvents
	}];
var dataExport = {/*
	'fDate' : $("#fDate").val(),*/
	'fOperation' : $("#fOperation").val(),
	'fMethod' : $("#fMethod").val(),
	'fIP' : $("#fIP").val(),
	'fLevel' : $("#fLevel").val(),
	'fParams' : $("#fParams").val(),
	'fBillerName' : $("#fBillerName").val(),
	'fExplanation' : $("#fExplanation").val(),
	'row' : 10000000
};

var colArr = [];
/*
 * @Description: 预加载 
 * @param :
 */
$(function (){
	resizeIframeHight();
//	initTable();
	initField();
	loadRecordName("RA",initTable);
	$(".RA",parent.document).click(function(){
		$('#inTable').bootstrapTable('refresh');
	});
});

/*
 * @Description: 初始化委外转出/接收表
 * @param : 
 */
function initTable(){
	$("#inTable").bootstrapTable('destroy').bootstrapTable({
		url : pathUrl("subcinentry/onCheckSubInList.action"),
		height : $(window).height()*0.78,//行高
		method : "POST",
		striped : true,
		cache : false,
		editable:true,//开启编辑模式
		sidePagination : "server", // 分页方式：client客户端分页，server服务端分页（*）
		pagination : true,
		pageNumber : 1,
		pageSize : 10,
		pageList : [ 10, 30, 60, 100, 'ALL' ],
		dataExport : dataExport,
		showExport: true,
		colArr : colArr,
		exportDataType: "allServer",
		buttonsAlign:"right",  //按钮位置
		rowStyle : function(row, index) {
			var classes = [ 'success' ];
			if (index == rowIndex) {
				return {
					classes : classes[0]
				};
			}
			return {};
		},
		queryParams : function(params) {
			var data = {};
			var orderBy = params.sort + " " + params.order;      //排序列名
			if(params.sort==undefined){
				orderBy = "fDate desc";
			}
			data.offset = params.offset +1;
			data.row = params.limit,
			data.sqlStr = sqlCombine;
			data.orderBy = orderBy;
			return data;
		},
		dataType : 'JSON',
		uniqueId : "id",
		columns : customColumn,
		onClickRow: function (row,$element) {
//			console.log(row)
			$('.success').removeClass('success');// 去除之前选中的行的，选中样式
			$($element).addClass('success');// 添加当前选中的 success样式用于区别
			index = $element.data('index');
			rowIndex = index;
			_index = index;
			page = $('.pagination .active').find("a").eq(0).text();
		},
		onLoadSuccess:function(data){
			console.log(data);
		},
		onPageChange : function(number, size) {
			if (page == number) {
				rowIndex = index;
			} else {
				$('.success').removeClass('success');
				rowIndex = "%^%%%%";
			}
		},
		exportHandler : function(text){
			if(text == 'success'){
				$('#export').text('导出成功');
				setTimeout(function(){
					$('#export').text('导出');
					$('#export').blur();
					$('#export').removeAttr('disabled');
				},3000)
			}
		}
	});
}


/*
 * @Description: 重置按钮点击事件 
 * @param :
 */
$(".resetting").click(function(){
	$(".clean_C").val("");
	var data = dataExport;
	for(prop in data){
		if(data.hasOwnProperty(prop)){
			data[prop] = '';
		}
	}
	reset();
});

/*
 * @Description: 重置
 * @param :
 */
function reset(){
	$("#infoTable").bootstrapTable('destroy'); 
	initTable();
	$('.success').removeClass('success');
	rowIndex = "%^%%%%";
}
/*
 * @Description: more按钮点击事件
 * @param : 
 */
$("#more").click(function(){
	if(flag){
		$("#more").text("隐藏");
		$("#extend").show();
		flag = false;
	}else{
		$("#more").text("更多");
		$("#extend").hide();
		flag = true;
	}
	$('#inTable').bootstrapTable('resetWidth');
});

/*
 * @Description: 数据内容更改赋值给dataExport
 * @param :
 */
$('.page-heading .clean_C').on('change',function(){
	var attr = $(this).attr('name');
	dataExport[attr] = $(this).val();
});
/*
 * @Description: 导出按钮点击事件
 * @param :
 */
$('#export').click(function(e){
	$('#inTable').exportColumn();
});

/*
 * @Description: 查询条件按回车事件
 * @param :
 */
$("#fDate,#fOperation,#fMethod,#fIP,#fLevel,#fParams,#fBillerName,#fExplanation").keypress(function(event){
	if(event.keyCode == "13"){
		initTable();
	}
});

/*
 * @Description: 空字符串格式化
 * @param :
 */
function isNull(value,row,index){
	if(value == null){
		return "";
	}else{
		return value;
	}
}
/*
 * @Description: 确认
 * @param :
 */
$("#confirm").click(function(){
	var fReceiveQty = parseFloat($("#auditingfReceiveQty").val());//接受数
	var fStockCheckQty = $("#auditingfStockCheckQty").val();//良品数
    var ngQty =parseFloat($("#auditingngfAuxQtyScrap").val())+parseFloat($("#auditingngfAuxQtyForItem").val())+parseFloat($("#auditingngfOthQty").val());
    var shiQty =ngQty+parseFloat($("#auditingfStockCheckQty").val());
    console.log(shiQty);
    console.log(ngQty);
	if(fStockCheckQty == ""){
		swal_Tips("实收良品数不能为空！");
		return false;
	}
	/*if(ngQty == ""){
		swal_Tips("实收不良品数不能为空！");
		return false;
	}*/
	fStockCheckQty = parseFloat(fStockCheckQty);
	/*ngQty = parseFloat(ngQty);*/
	if(isNaN(fStockCheckQty)){
		swal_Tips("请输入正确的实收良品数！");
		return false;
	}
	if(shiQty <= 0){
		swal_Tips("实收良品数+工废数+料废数+次品数不能为0！");
		return false;
	}
	if(fStockCheckQty > fReceiveQty){
		swal_Tips("实收良品数不能大于接收数！");
		return false;
	}
	if(isNaN(ngQty)){
		swal_Tips("请输入正确的实收不良品数！");
		return false;
	}
	if(ngQty > fReceiveQty){
		swal_Tips("实收工废数+料废数+次品数不能大于接收数！");
		return false;
	}
	if((shiQty) > fReceiveQty){
		swal_Tips("实收良品数加实收+工废数+料废数+次品数不能大于接收数！");
		return false;
	}
	//console.log(_fEntryID,_fInterID,_fIcomEntryId,fStockCheckQty,ngQty)
	showLoading();
	$.ajax({
		url : pathUrl("subcinentry/checkSubIn.action"),
		type : 'POST',
		dataType : "JSON",
		data : {
			"fEntryID" : _fEntryID,
			"fInterID" : _fInterID,
			"fIcomEntryId" : _fIcomEntryId,
			"fOperSN" : _fOperSN,
			"fStockCheckQty" : fStockCheckQty,
			"fSubcOutId" : _fSubcOutId,
			"fStockID" : $("#fStockID").val(),
			"ngQty" : ngQty
		},
		success : function(data) {
			if(data.code == "0000"){
				if(data.desc == 'dy'){
					getReport('wms',data.id,0);
				}
				reseting();
				$("#auditingModal").modal('hide');
				swal_Tips_Short(data.message);
			}else{
				swal_Tips(data.message);
			}
		},
		error : function(request) {
			swal_Tips_Error();
		}
	});
});
function reseting(){
	initTable();
	$('.success').removeClass('success');
	rowIndex = "%^%%%%";
}